/*
 * Copyright (C) 2021 Baidu, Inc. All Rights Reserved.
 */
package com.chukun.rpc.provider;

import javax.annotation.Resource;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.chukun.rpc.core.TinyRpcProperties;
import com.chukun.rpc.registry.RegistryService;
import com.chukun.rpc.registry.RegistryType;
import com.chukun.rpc.registry.ServiceRegistryFactory;

/**
 * <p>配置TinyRpcProvider bean<p/>
 *
 * @author chukun
 * @version 1.0
 * @since 1.0-SNAPSHOT
 */
@Configuration
@EnableConfigurationProperties(TinyRpcProperties.class)
public class TinyRpcProviderAutoConfiguration {

    @Resource
    private TinyRpcProperties properties;

    @Bean
    public TinyRpcProvider rpcProvider() {
        RegistryType type = RegistryType.valueOf(properties.getRegisterType());
        RegistryService registryService = ServiceRegistryFactory.getInstance(properties.getRegistryAddr(), type);
        return new TinyRpcProvider(properties.getServicePort(), registryService);
    }
}
